// pages/marketing/wholeCustom/wholeCustom.js
import {
  smsHomeNewProduct
} from '../../../../config/config'
var configUrl = require('../../../../config/config') //引入配置接口文件
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: getApp().globalData.imgUrl,
    wholeCustomTitle: getApp().globalData.BgimgUrl + 'wholeCustomTitle.png', // 头部背景图
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    listCur: '',
    typeData: [],
    page: 1,
    rows: 10,
    cat: '',
    nomoreData: false,
    listData: [],
    addrCode: {},
    titleHeight: '',
    marginTop: '',
    /**-合伙人--*/
    isShowPartnerAll: 1,
    showPartnerCodePop: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (!wx.getStorageSync('loginMsg').token) {
      app.initLogin() //初始化登录
    }
    let that = this
    wx.getSystemInfo({
      success(res) {
        let statusBarHeight = res.statusBarHeight + 65
        let marginTop = res.statusBarHeight + 105
        that.setData({
          titleHeight: statusBarHeight + 'px',
          marginTop: marginTop + 'px'
        })
      }
    })
    let addrCode = wx.getStorageSync('addrCode')
    that.setData({
      addrCode: addrCode
    })
    that.gettypeData()
    if (options.hid) {
      var hid = options.hid
        var stInFun = setInterval(() => {
          if (wx.getStorageSync('loginMsg').token && wx.getStorageSync('addrCode').provinceCode) {
            clearInterval(stInFun)
            if (!wx.getStorageSync('loginMsg').loginState) {
              if (wx.getUserProfile) {
                app.getUserProfile(function(){
                  that.setData({
                    getPhoneNumberData:{
                      isShowGetPhoneNumber:true
                    }
                  })
                })
              } else {
                if(!wx.getStorageSync('loginMsg').bindingPhone){
                  that.setData({
                    getPhoneNumberData:{
                      isShowGetPhoneNumber:true
                    }
                  })
                }
              }
              // this.setData({
              //   isShowGetUserInfo: true
              // })
            }
            const reloadListFlag = true
            that.gettypeData(reloadListFlag)
            app.bindingPartner(hid)
          }
        }, 1000)
    }
  },
  bindGetUserInfo: function (e) { //登录-注册
    var that=this;
    app.getUserInfo()
    this.setData({
      showSetting: false,
      isShowGetUserInfo: false
    })
    var setFun=setInterval(()=>{
      if(wx.getStorageSync('showGetPhone')){
        that.setData({
          getPhoneNumberData:{
            isShowGetPhoneNumber:true,
          },
          showSetting: false,
          isShowGetUserInfo: false
        })
        wx.removeStorageSync('showGetPhone')
        clearInterval(setFun)
      }
    },1000)
    
  },
  onCloseShowGetUserInfo() { //关闭弹出层
    this.setData({
      showSetting: false,
      isShowGetUserInfo: false
    })
  },
   /**绑定手机号 ---str*/
   closeGetphone(){
    this.setData({
      getPhoneNumberData:{
        isShowGetPhoneNumber:false

      }
    })
  },
  getPhoneNumber(e){
    var that=this;
    let loginMsg = wx.getStorageSync('loginMsg')
    if (e.detail.iv && e.detail.encryptedData) {
      let option = {}
      option.encryptedData = e.detail.encryptedData
      option.iv = e.detail.iv
      option.openid = loginMsg.openid
      option.session_key = loginMsg.session_key
      app.reqFun.requestJson('POST',configUrl.decryptGetPhone,option,(res)=>{
        if(res.data.success==1){
          that.setData({
            getPhoneNumberData:{
              isShowGetPhoneNumber:false,
            }
          })
          wx.showToast({
            title: '绑定成功。',
          })
        }
      }) 
    } else {
      console.log('取消')
    }
  },
 
  listSelect(e) {
    this.setData({
      listCur: e.currentTarget.dataset.id,
      cat: e.currentTarget.dataset.id,
      page: 1,
      rows: 10,
      nomoreData: false,
      listData: []
    })
    this.getsmsHomeNewProduct(e.currentTarget.dataset.id)
  },
  // 精选专区
  getsmsHomeNewProduct(cat, reloadListFlag) {
    let that = this
    let option = {}
    option.page = that.data.page
    option.rows = that.data.rows
    option.callFrom = 'HOUSE'
    option.provinceCode = that.data.addrCode.provinceCode
    option.cat = cat
    app.reqFun.requestJson('POST', smsHomeNewProduct, option, (res) => {
      if (res.data.success == 1) {
        let records = res.data.data.solrProductDtos.records
        if (records.length < that.data.rows) {
          that.setData({
            nomoreData: true
          })
        }
        if (reloadListFlag) {
          that.setData({
            listData: records
          })
        } else {
          that.setData({
            listData: that.data.listData.concat(records)
          })
        }
      }
    })
  },
  gettypeData(reloadListFlag) {
    let that = this
    let option = {}
    option.page = that.data.page
    option.rows = that.data.rows
    option.callFrom = 'HOUSE'
    option.provinceCode = that.data.addrCode.provinceCode
    app.reqFun.requestJson('POST', smsHomeNewProduct, option, (res) => {
      if (res.data.success == 1) {
        let productCategoryFacetDtos = res.data.data.productCategoryFacetDtos
        let type = {
          id: '',
          name: '全部'
        }
        productCategoryFacetDtos.unshift(type)
        that.setData({
          typeData: productCategoryFacetDtos,
          listCur: productCategoryFacetDtos[0].id,
          cat: productCategoryFacetDtos[0].id,
        })
        that.getsmsHomeNewProduct(productCategoryFacetDtos[0].id, reloadListFlag)
      }
    })
  },
  // 返回
  goreBack(e) {
    wx.navigateBack({
      delta: 1,
    })
  },
  // 跳转商品详情
  goGoodsInfo(option) {
    let skuid = option.currentTarget.dataset.skuid
    wx.navigateTo({
      url: '/packageShop/pages/goodsInfo/goodsInfo?id=' + skuid,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    setTimeout(()=>{
      this.setData({
        partnerInfo:getApp().globalData.partnerInfo
      })
      },500)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.nomoreData) {
      this.setData({
        page: this.data.page + 1
      })
      this.getsmsHomeNewProduct(this.data.cat)
    }
  },
  goHome: function (e) { //返回主页
    wx.reLaunch({
      url: '/pages/newIndex/newIndex'
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '全屋定制',
      desc: '全屋定制',
      path: '/packagBuy/pages/marketing/wholeCustom/wholeCustom?hid=' + wx.getStorageSync('loginMsg').userId
    }
  },

  //展开合伙人信息
  showPartnerAll: function () {
    let isShowPartnerAll;
    if (this.data.isShowPartnerAll == 1) {
      isShowPartnerAll = 0;
    } else {
      isShowPartnerAll = 1;
    }
    this.setData({
      isShowPartnerAll: isShowPartnerAll,
    })
  },
  //展示合伙人二维码
  partnerCode: function (e) {
    // this.setData({
    //   showPartnerCodePop: true,
    // })
    let codeImg=e.target.dataset.url
    wx.previewImage({
      urls:[codeImg],
      current: codeImg, // 当前显示图片的http链接
     })
  },
  //关闭合伙人二维码
  closeCodePop: function () {
    console.log("3253456678dsgudkdkgfksfvkjdsvbjkdsvbvkj")
    this.setData({
      showPartnerCodePop: false,
    })
  },
  //拨打合伙人电话
  partnerPhone: function () {
    let that = this;
    wx.makePhoneCall({
      phoneNumber: that.data.partnerInfo.partnerPhone,
    })
  }
})